package tech.aistar.corejava.day11.homework;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 李雨昂 on 2019/7/29.
 */
public class BookDaoImpl implements IBookDao{
    //数据的容器
    List<Book> books = new ArrayList<>();


    public BookDaoImpl(){

    }
    @Override
    public void save(Book b) {
        books.add(b);
    }

    @Override
    public List<Book> findAll() {
        return books;
    }


    @Override
    public void delById(Integer id) {
        if(books.isEmpty()){
            System.out.println("没有可删的图书");
        }

        Iterator<Book> iter = books.iterator();
        while(iter.hasNext()){
            Book b = iter.next();
            if(b.getId() == id){
                iter.remove();

            }

        }
        System.out.println(books);
    }

    @Override
    public void delByPrice(double price) {
        if(books.isEmpty()){
            System.out.println("没有可删的图书");
        }
        for (int i = books.size() - 1; i >= 0; i--) {
            if(books.get(i).getPrice() > price){
                books.remove(i);

            }
        }
        System.out.println(books);

    }
}
